package cn.fanqinghong.ymcc.mq.consumer;

import cn.fanqinghong.ymcc.domain.MediaFile;
import cn.fanqinghong.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import io.netty.util.CharsetUtil;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@RocketMQMessageListener(
        consumerGroup= "media-consumer-group",
        topic= "media-topic",
        selectorExpression= "media-tags"
)
public class MessageConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    @Transactional
    public void onMessage(MessageExt message) {
        String jsonStr = new String(message.getBody(), CharsetUtil.UTF_8);
        //把接收到的消息转为MediaFile格式
        MediaFile mediaFile = JSONObject.parseObject(jsonStr, MediaFile.class);
        mediaFileService.handleFile2m3u8(mediaFile);
    }
}